<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\adminapi\validate\setting;

use think\Validate;

class ShippingTemplatesValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'name' => 'require',
        'place_address' => 'require', // Updated rule based on data value
        'total_fee' => 'in:0,1', // Updated rule based on data value
        'region' => 'array', // Updated rule based on data value
        'free' => 'array', // Updated rule based on data value
        'free_shipping_type' => 'in:1,2,3',
        'min_items_for_free_shipping' => 'integer',
        'min_cost_for_free_shipping' => 'string',
        'price_type' => 'in:1,2,3',
        'first' => 'integer',
        'first_price' => 'string',
        'continue' => 'integer',
        'continue_price' => 'string',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
        'name.require' => '400025',
        'place_address.require' => '30079', // Updated error code based on data value
        'total_fee.in' => '30081', // Updated error code based on data value
        'region.array' => '30082', // Updated error code based on data value
        'free.array' => '30083', // Updated error code based on data value
        'free_shipping_type.in' => '30084',
        'min_items_for_free_shipping.integer' => '30085',
        'min_cost_for_free_shipping.string' => '30086', // 字符串类型错误提示
        'price_type.in' => '30087',
        'first.integer' => '30088',
        'first_price.string' => '30089',
        'continue.integer' => '30090',
        'continue_price.string' => '30091',
    ];

    protected $scene = [
        'save' => ['name', 'place_address', 'total_fee', 'region', 'free', 'free_shipping_type', 'min_items_for_free_shipping', 'min_cost_for_free_shipping', 'price_type', 'first', 'first_price', 'continue', 'continue_price'],
    ];

    /**
     * 动态设置验证规则
     *
     * @param array $data
     * @return void
     */
    public function setRules(array $data)
    {
        // 根据 $data 中的值动态调整验证规则
        if (isset($data['free_shipping_type'])) {
            $this->rule['min_items_for_free_shipping'] .= '|required_if:free_shipping_type,1,2';
            $this->rule['min_cost_for_free_shipping'] .= '|required_if:free_shipping_type,2';
        }
    }
}